package com.briup.cms.service.impl;



import com.briup.cms.bean.Article;
import com.briup.cms.bean.extend.ArticleExtend;
import com.briup.cms.dao.extend.ArticleExtendMapper;
import com.briup.cms.dao.ArticleMapper;
import com.briup.cms.exception.CustomerException;
import com.briup.cms.service.IArticleService;
import com.briup.cms.utils.MessageStatus;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class ArticleServiceImpl implements IArticleService {
    @Autowired
    private ArticleMapper articleMapper;
    @Autowired
    private ArticleExtendMapper articleExtendMapper;

    public ArticleServiceImpl() {
    }

    public ArticleExtend findByIdWithCategoryAndUser(int id) {
        ArticleExtend byIdWithCategoryAndUser = articleExtendMapper.findByIdWithCategoryAndUser(id);
        return byIdWithCategoryAndUser;
    }

    @Transactional
    public void saveOrUpdate(Article article) throws CustomerException {
        if (article.getArticleId() != null) {
            Article updateArticle = articleMapper.selectByPrimaryKey(article.getArticleId());
            if (updateArticle == null) {
                throw new CustomerException(MessageStatus.SPECIFIED_QUESTIONED_ARTICLE_NOT_EXIST);
            }
            articleMapper.updateByPrimaryKeySelective(article);
        } else {
            article.setArticlePublishTime(new Date());
            article.setArticleStatus(0);
            article.setArticleThumpUp(0);
            article.setArticleReadTimes(0);
            articleMapper.insert(article);
        }

    }

    public PageInfo<ArticleExtend> pageQueryArticleWithCategoryAndUser(int pageNum, int pageSize, String title, Integer status, Integer categoryId) throws CustomerException {
        PageHelper.startPage(pageNum, pageSize, true);
        List<ArticleExtend> list = this.articleExtendMapper.queryArticleWithCategoryAndUser(title, status, categoryId);
        if (list == null) {
            list = new ArrayList();
        }
        return new PageInfo((List)list);
    }

    @Transactional
    public void deleteById(int id) throws CustomerException {
        this.articleMapper.deleteByPrimaryKey(id);
    }

    public PageInfo<ArticleExtend> pageQueryRecommend(int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize, true);
        List<ArticleExtend> list = articleExtendMapper.findCategoryByStatus(3);
        return new PageInfo(list);
    }

    @Transactional
    public void changeStatus(int id, int status) throws CustomerException {
        Article artileFromDB = articleMapper.selectByPrimaryKey(id);
        if (artileFromDB == null) {
            throw new CustomerException(MessageStatus.SPECIFIED_QUESTIONED_ARTICLE_NOT_EXIST);
        } else {
            Article article = new Article();
            article.setArticleId(id);
            article.setArticleStatus(status);
            articleMapper.updateByPrimaryKeySelective(article);
        }
    }
}
